import React from 'react';
import {Card, Button, Form, Select, Modal, message, Table} from 'antd';
import axios from './../../axios';
import Utils from './../../utils/utils';

const FormItem = Form.Item;
const Option = Select.Option;
export default class City extends React.Component {
    state = {
        loading: true,
        dataSource: [],
        isShowOpenCity: false
    };

    params = {
        page: 1
    };

    componentDidMount() {
        this.request();
    }

    request = () => {
        let that = this;
        axios.Axios({
            url: 'open/city',
            method: 'get',
            params: {
                page: this.params.page
            }
        }).then(res => {
            this.setState({
                dataSource: res.list,
                loading: false,
                pagination: Utils.pagination(res, (current) => {
                    that.params.page = current;
                    this.request()
                })
            })
        })
    };
    // modal 显示
    handleOpenCity = () => {
        this.setState({
            isShowOpenCity: true
        })
    };
    // 城市开通submit
    handleSubmit = () => {
        let cityInfo = this.cityForm.props.form.getFieldsValue();
        console.log(cityInfo);
        axios.Axios({
            url: 'city/open',
            method: 'post',
            data: cityInfo
        }).then(res => {
            message.success(res.message);
            this.setState({
                isShowOpenCity: false
            });
            this.request()
        })
    };


    render() {

        const columns = [
            {
                title: '城市ID',
                key: 'id',
                dataIndex: 'id'
            }, {
                title: '城市名称',
                key: 'name',
                dataIndex: 'name'
            }, {
                title: '用车模式',
                key: 'mode',
                dataIndex: 'mode',
                render(mode) {
                    return mode === 1 ? '停车点' : '禁停区';
                }
            }, {
                title: '营运模式',
                key: 'op_mode',
                dataIndex: 'op_mode',
                render(op_mode) {
                    return op_mode === 1 ? '自营' : '加盟';
                }
            }, {
                title: '授权加盟商',
                key: 'franchisee_name',
                dataIndex: 'franchisee_name'
            }, {
                title: '城市管理员',
                key: 'city_admins',
                dataIndex: 'city_admins',
                render(arr) {
                    return arr.map((item) => {
                        return item.user_name;
                    }).join(',');
                }
            }, {
                title: '城市开通时间',
                key: 'open_time',
                dataIndex: 'open_time'
            }, {
                title: '操作时间',
                key: 'update_time',
                dataIndex: 'update_time'
            }, {
                title: '操作人',
                key: 'sys_user_name',
                dataIndex: 'sys_user_name'
            }
        ];

        return (
            <div className="wrap">
                <Card>
                    <FilterForm/>
                </Card>
                <Card style={{marginTop: 10}}>
                    <Button type="primary" onClick={this.handleOpenCity}>开通城市</Button>
                </Card>
                <div className="content-wrap">
                    <Table
                        bordered
                        columns={columns}
                        dataSource={this.state.dataSource}
                        pagination={this.state.pagination}
                        rowKey={record => record.id}
                    />
                </div>
                <Modal
                    title="开通城市"
                    visible={this.state.isShowOpenCity}
                    onCancel={() => {
                        this.setState({
                            isShowOpenCity: false
                        })
                    }}
                    onOk={this.handleSubmit}
                >
                    <OpenCityForm wrappedComponentRef={(inst) => {
                        this.cityForm = inst;
                    }}/>
                    {/* wrappedComponentRef === ref */}
                </Modal>
            </div>
        )
    }
}

class FilterForm extends React.Component {

    render() {
        const {getFieldDecorator} = this.props.form;
        return (
            <Form layout="inline">
                <FormItem label="城市">
                    {
                        getFieldDecorator('city_id')(
                            <Select
                                style={{width: 100}}
                                placeholder="全部"
                            >
                                <Option value="">全部</Option>
                                <Option value="1">北京市</Option>
                                <Option value="2">天津市</Option>
                                <Option value="3">深圳市</Option>
                            </Select>
                        )
                    }
                </FormItem>
                <FormItem label="用车模式">
                    {
                        getFieldDecorator('mode')(
                            <Select
                                style={{width: 120}}
                                placeholder="全部"
                            >
                                <Option value="">全部</Option>
                                <Option value="1">指定停车点模式</Option>
                                <Option value="2">禁停区模式</Option>
                            </Select>
                        )
                    }
                </FormItem>
                <FormItem label="营运模式">
                    {
                        getFieldDecorator('op_mode')(
                            <Select
                                style={{width: 80}}
                                placeholder="全部"
                            >
                                <Option value="">全部</Option>
                                <Option value="1">自营</Option>
                                <Option value="2">加盟</Option>
                            </Select>
                        )
                    }
                </FormItem>
                <FormItem label="加盟商授权状态">
                    {
                        getFieldDecorator('auth_status')(
                            <Select
                                style={{width: 100}}
                                placeholder="全部"
                            >
                                <Option value="">全部</Option>
                                <Option value="1">已授权</Option>
                                <Option value="2">未授权</Option>
                            </Select>
                        )
                    }
                </FormItem>
                <FormItem>
                    <Button type="primary" style={{margin: '0 20px'}}>查询</Button>
                    <Button>重置</Button>
                </FormItem>
            </Form>
        );
    }
}

FilterForm = Form.create({})(FilterForm);

class OpenCityForm extends React.Component {
    render() {
        const formItemLayout = {
            labelCol: {
                span: 5
            },
            wrapperCol: {
                span: 10
            }
        };
        const {getFieldDecorator} = this.props.form;
        return (
            <Form layout="horizontal">
                <FormItem label="选择城市" {...formItemLayout}>
                    {
                        getFieldDecorator('city_id', {
                            initialValue: '1'
                        })(
                            <Select>
                                <Option value="">全部</Option>
                                <Option value="1">北京市</Option>
                                <Option value="2">天津市</Option>
                            </Select>
                        )
                    }
                </FormItem>
                <FormItem label="营运模式" {...formItemLayout}>
                    {
                        getFieldDecorator('op_mode', {
                            initialValue: '1'
                        })(
                            <Select>
                                <Option value="1">自营</Option>
                                <Option value="2">加盟</Option>
                            </Select>
                        )
                    }
                </FormItem>
                <FormItem label="用车模式" {...formItemLayout}>
                    {
                        getFieldDecorator('use_mode', {
                            initialValue: '1'
                        })(
                            <Select>
                                <Option value="1">指定停车点</Option>
                                <Option value="2">禁停区</Option>
                            </Select>
                        )
                    }
                </FormItem>
            </Form>
        );
    }
}

OpenCityForm = Form.create({})(OpenCityForm);